package com.cvicse.jrcompetition01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cvicse.jrcompetition01.entity.Department;
import com.cvicse.jrcompetition01.entity.Mechanism;
import com.cvicse.jrcompetition01.entity.MechanismDepartment;
import com.cvicse.jrcompetition01.mapper.MechanismDepartmentMapper;
import com.cvicse.jrcompetition01.mapper.MechanismMapper;
import com.cvicse.jrcompetition01.service.MechanismDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者:kissshotwang@163.com
 * @since 2022-07-23
 */
@Service
public class MechanismDepartmentServiceImpl extends ServiceImpl<MechanismDepartmentMapper, MechanismDepartment> implements MechanismDepartmentService {
    @Resource
    MechanismMapper mechanismMapper;

    @Resource
    MechanismDepartmentMapper mechanismDepartmentMapper;

    @Override
    public Integer add(Department department) {
        //中间表的记录插入
        MechanismDepartment mechanismDepartment = new MechanismDepartment();
        //1.根据”新增部门“时的机构编号查询到机构的主键id
        Mechanism selectOne = mechanismMapper.selectOne(new QueryWrapper<Mechanism>().eq("mechanism_number", department.getMechanismDepartmentNumber().substring(0, 10)));
        //2.Department创建之后可以拿到此Department的主键id
        mechanismDepartment.setMechanismId(selectOne.getId());
        mechanismDepartment.setDepartmentId(department.getId());
        int insert = mechanismDepartmentMapper.insert(mechanismDepartment);
        return insert;

    }

}
